﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP09S03_OfficeTracking.Controller;
using WP09S03_OfficeTracking.DAL;

namespace WP09S03_OfficeTracking
{
    public partial class FrmEquipmentReturn : WP09S03_OfficeTracking.ParentClass.ParentForm
    {
        TransactionController tran = new TransactionController();
        ItemController itemCtrl = new ItemController();
        private string itemDetailID = "";
        public FrmEquipmentReturn()
        {
            InitializeComponent();
            DataBinding_DeviceStatus();
            InitSetting();
        }

        private void InitSetting()
        {
            this.issueTransactionUC.rbNo.Checked = true;
            this.issueTransactionUC.rbNo.Enabled = false;
            this.issueTransactionUC.rbYes.Enabled = false;
            this.issueTransactionUC.cbbStatus.SelectedIndex = 2;
            this.issueTransactionUC.cbbStatus.Enabled = false;
            this.issueTransactionUC.gbSearchOption.Text = "Issue Transaction Search Option";
        }

        private void DataBinding_DeviceStatus()
        {
            DSItem.DeviceStatusDataTable table = new DSItem.DeviceStatusDataTable();
            table = itemCtrl.GetDeviceStatus();
            this.cbbDeviceStatus.DataSource = table;
            this.cbbDeviceStatus.DisplayMember = "DeviceStatus";
            this.cbbDeviceStatus.ValueMember = "DeviceStatusID";
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.issueTransactionUC.dgvData.Rows.Count == 0)
                {
                    SetMessage("Please select issue transaction.");
                    return;
                }
                else
                {
                    SetMessage("");
                }
                this.txtIssueTransactionID.Text = this.issueTransactionUC.dgvData.SelectedRows[0].Cells[0].Value.ToString();
                this.itemDetailID = this.issueTransactionUC.dgvData.SelectedRows[0].Cells[4].Value.ToString();
                IsEnableSubmit();

            }
            catch (Exception err)
            {
                CatchError(err);
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string issueTranID = this.txtIssueTransactionID.Text.Trim();
                if (issueTranID == "")
                {
                    SetMessage("Invalid Issue Transaction ID.");
                    return;
                }
                DSTransaction.vw_IssueTransactionDataTable table = tran.GetIssueTransaction(issueTranID, "", "",null , null, null,null, "", "", "", "", "", "", "");
                if (table.Rows.Count == 0)
                {
                    SetMessage("Invalid Issue Transaction ID.");
                    return;
                }

                int deviceID = Convert.ToInt32(this.cbbDeviceStatus.SelectedValue.ToString());
                int deviceStatusID = Convert.ToInt32(this.cbbDeviceStatus.SelectedValue.ToString());
                string remark = this.txtRemark.Text;
                bool result = tran.ReturnTransactionBookAndEquipment(issueTranID, itemDetailID, deviceID, deviceStatusID, remark);

                if (result)
                {
                    clearTransactionInfo();
                    SetMessage("Transaction is successful.");
                }
                else
                {
                    SetMessage("Transaction is fail.");
                }
            }
            catch (Exception err)
            {
                SetMessage("Transaction is fail." + err.Message);
                CatchError(err);
            }
        }

        private void clearTransactionInfo()
        {
            this.txtIssueTransactionID.Text = "";
            this.issueTransactionUC.BindData_DGV();
        }

        private void FrmEquipmentReturn_Load(object sender, EventArgs e)
        {
            this.btnSelect.Enabled = false;
            IsEnableSubmit();
            this.issueTransactionUC.dgvData.SelectionChanged += dgvData_SelectionChanged;
        }

        private void dgvData_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.issueTransactionUC.dgvData.SelectedRows.Count > 0)
                {
                    this.btnSelect.Enabled = true;
                }
                else
                {
                    this.btnSelect.Enabled = false;
                }

            }
            catch (Exception err)
            {
                CatchError(err);
            }
        }

        private void IsEnableSubmit()
        {
            if (this.txtIssueTransactionID.Text.Trim() != "")
            {
                this.btnSubmit.Enabled = true;
            }
            else
            {
                this.btnSubmit.Enabled = false;
            }
        }
    }
}
